// 搜索模块-mysql服务处理

// 引入数据库模块
const db = require('../config/mysql.js')
const utils = require("../common/utils")

// 是否好友
const isFriend = (id, email) => {
    return new Promise((resolve, reject) => {
        let sql = `select friend_status from user, friend where id = "${id}" and id = user_id and friend_id = (select id from user where email = "${email}")`
        db.query(sql, (err, result) => {
          if (err) return reject(err)
          if (result.length == 1) {
            if (result[0].friend_status == 2) {
              return resolve(true)
            }
          }
          return resolve(false)
        })
    })
}

// 查询用户信息
const getFriendInfo = (email) => {
  return new Promise((resolve, reject) => {
    let sql = `select id, user_name, email, gender, birthday, sign, avatar, register_time from user where email = "${email}"`
    db.query(sql, (err, result) => {
      if (err) return reject(err)
      if (result.length == 1) {
        return resolve(result[0])
      }
      return resolve({})
    })
  })
}

// 获取用户详情
const getFriendDetail = (id) => {
  return new Promise((resolve, reject) => {
    let sql = `select id, user_name, email, gender, birthday, sign, avatar, register_time from user where id = ${id}`
    db.query(sql, (err, result) => {
      if (err) return reject(err)
      if (result.length == 1) {
        return resolve(result[0])
      }
      return resolve({})
    })
  })
}

// 是否有昵称
const hasNickName = (uid, fid) => {
  return new Promise((resolve, reject) => {
    let sql1 = `select user_id, friend_id, nick_name from friend where user_id = "${uid}" and friend_id = "${fid}"`
    db.query(sql1, (err1, result1) => {
      if (err1) return reject(err1)
      if (result1.length == 1) return resolve(result1[0].nick_name)
      return resolve('')
    })
  })
}

module.exports = {
  isFriend,
  getFriendInfo,
  getFriendDetail,
  hasNickName
}
